#include "nowplaylistview.h"
#include "ui_nowplaylistview.h"
#include <QPainter>
#include <QDebug>
#include <QTreeWidgetItem>
#include "playEngine/ezvoiceplayermanager.h"
#include "playlistbasemodel.h"
#include "EZTallyArray.h"
#include "songmodel.h"
#include "log/EZQLOG.h"
#include "common/songMenu/songmenu.h"


NowPlayListView::NowPlayListView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NowPlayListView)
{
    ui->setupUi(this);
//    this->setModal(true);
//    setWindowFlags(Qt::FramelessWindowHint|Qt::Dialog);
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground,true);
//    QPalette pal= this->palette();
//    pal.setColor(QPalette::Background, QColor(100,100,100,50));
//    setPalette(pal);
//    setAutoFillBackground(true);
//    setWindowOpacity(1);

//    ui->pushButton->setStyleSheet(tr(""));
//    ui->pushButton->setPalette(pal);;
//    ui->pushButton->setAutoFillBackground(true);
    _oldIndex = -1;
    EZVoicePlayerManager *playManager = EZVoicePlayerManager::defaultManager();
    connect(playManager,SIGNAL(sigPlayListContentChange()),this,SLOT(onPlayListContentChange()));
    connect(playManager,SIGNAL(sigPlayIndexChange(int)),this,SLOT(onPlayIndexChange(int)));

    connect(ui->treeWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(itemDoubleClicked(QTreeWidgetItem*,int)));
    reView();

    ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->treeWidget,SIGNAL(customContextMenuRequested(const QPoint &)),this,SLOT(slot_customContextMenuRequested(const QPoint&)));
}

NowPlayListView::~NowPlayListView()
{
    delete ui;
}

void NowPlayListView::focusInEvent(QFocusEvent * event)
{
    EZDebug << "focusInEvent" <<event;
}

//void NowPlayListView::paintEvent(QPaintEvent *)
//{
//    QPainter p(this);
//     p.fillRect(rect(), QColor(0,0,0,0));
//}

void NowPlayListView::mousePressEvent(QMouseEvent *)
{
    hide();
}

void NowPlayListView::setFitPoint(const QPoint &/*fitpoint*/)
{
//    QRect rect = ui->treeWidget->geometry();
//    rect.setX( fitpoint.x() - ui->treeWidget->width()/2 );
//    rect.setY( fitpoint.y() );
//    rect.setWidth(350);
//    rect.setHeight(450);
////    qDebug() << rect;
//    ui->treeWidget->setGeometry(rect);
}

void NowPlayListView::onPlayListContentChange()
{
    reView();
}

void NowPlayListView::reView()
{
    ui->treeWidget->clear();

    EZVoicePlayerManager *playerManager = EZVoicePlayerManager::defaultManager();
    PlayListBaseModel *playList = playerManager->getPlayList();
    if(playList==0){
        return;
    }
    EZTally::EZTallyArray<SongModel*> *songs = playList->getSongs();
    if(songs==0){
        return;
    }
    QList<QTreeWidgetItem*> itemList;
    for(int i=0;i<songs->Count();i++){
        SongModel *song = songs->ObjectAtIndex(i);
        QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
        item->setText(0,song->title());
        itemList << item;
    }
    ui->treeWidget->addTopLevelItems(itemList);
    onPlayIndexChange(playList->getCurIndex());
}

void NowPlayListView::onPlayIndexChange(int index)
{
    if(_oldIndex>=0 && _oldIndex < ui->treeWidget->topLevelItemCount())
    {
        QTreeWidgetItem *item = ui->treeWidget->topLevelItem(_oldIndex);
        item->setTextColor(0,QColor(0,0,0,255));
    }
    if(index>=0 && index < ui->treeWidget->topLevelItemCount()){
        QTreeWidgetItem *item = ui->treeWidget->topLevelItem(index);
        item->setTextColor(0,QColor(135,196,233,255));
    }
    _oldIndex = index;
}

void NowPlayListView::itemDoubleClicked(QTreeWidgetItem * item, int)
{
    int index = ui->treeWidget->indexOfTopLevelItem(item);
    EZVoicePlayerManager *playManager = EZVoicePlayerManager::defaultManager();
    playManager->playIndex(index);
}

void NowPlayListView::slot_customContextMenuRequested(const QPoint & pos)
{
//    QTreeWidgetItem *item = ui->treeWidget->itemAt(pos);
    QModelIndex index = ui->treeWidget->indexAt(pos);
    int row = index.row();

    QPoint point(pos.x(),ui->treeWidget->header()->height()+pos.y());

    SongAddToMyPlayListMenu spm;

    QMenu menu;
    QAction *addMenuAction = menu.addMenu(&spm);
    addMenuAction->setText("add to myPlayList");
    QAction *deleteAction = menu.addAction("delete song (假的)");

    QAction *selectAction = menu.exec(ui->treeWidget->mapToGlobal(point));
    if(selectAction==deleteAction){

    }else{

        EZVoicePlayerManager *playerManager = EZVoicePlayerManager::defaultManager();
        PlayListBaseModel *playList = playerManager->getPlayList();
       spm.setSong(playList->songAtIndex(row));

        spm.runActionEvent(selectAction);

    }
}
